home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / CyberStarter / Sources / SmrtPtr.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  1.9 KB  |  86 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //    
  3. //    File:                SmrtPtr.h
  4. //    Release Version:    $ ODF 2 $
  5. //    
  6. //    Author:                Mark Lanett
  7. //    
  8. //    Copyright (c) 1995 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //    
  10. //    Template smart pointer (envelope) class
  11. //    
  12. //========================================================================================
  13.  
  14. #ifndef _ODF_Smart_Pointers_
  15. #define _ODF_Smart_Pointers_
  16.  
  17. //-----------------------------------------------------------------------------
  18. // TPointer
  19. // This is an envelope class, more robust than TempObj (OD Utilities)
  20. // TempObj doesn't support operator=
  21. //-----------------------------------------------------------------------------
  22.  
  23. template <class T>
  24. class TPointer {
  25. public:
  26.     FW_DECLARE_AUTO (TPointer)
  27.     inline            TPointer ();
  28.     inline            TPointer (T* object);
  29.     inline            ~TPointer ();
  30.     inline TPointer<T>& operator= (T* object);
  31.     inline void     Assign (T* object);
  32.     inline T*         Release ();
  33. private:
  34.     T*                 fObject;
  35. private:
  36.                     TPointer (const TPointer<T>& other)    {}
  37.     TPointer<T>&    operator= (const TPointer<T>& other)    {return *this;}
  38. };
  39.  
  40. //-----------------------------------------------------------------------------
  41. // TPointer Inlines
  42. //-----------------------------------------------------------------------------
  43.  
  44. template <class T> inline 
  45. TPointer<T>::TPointer ()
  46. :    fObject (kODNULL)
  47. {
  48. }
  49.  
  50. template <class T> inline 
  51. TPointer<T>::TPointer (T* object)
  52. :    fObject (object)
  53. {
  54. }
  55.  
  56. template <class T> inline 
  57. TPointer<T>::~TPointer ()
  58. {
  59.     Assign (kODNULL);
  60. }
  61.  
  62. template <class T> inline 
  63. TPointer<T>& TPointer<T>::operator= (T* object)
  64. {
  65.     Assign (object); 
  66.     return *this;
  67. }
  68.  
  69. template <class T> inline 
  70. void TPointer<T>::Assign (T* object)
  71. {
  72.     delete fObject;
  73.     fObject = object;
  74. }
  75.  
  76. template <class T> inline 
  77. T* TPointer<T>::Release ()
  78. {
  79.     T* temporary = fObject;
  80.     fObject = kODNULL;
  81.     return temporary;
  82. }
  83.  
  84. #endif // _ODF_Smart_Pointers_
  85.  
  86.